package com.card.game;

import java.util.Collections;
import java.util.Random;
import java.util.Vector;

import com.card.game.room.RoomSetting;
import com.card.game.room.SHRoom;
import com.google.common.collect.Lists;
import com.xiaoao.net.utils.PubFunc;

/**
 * @Description: TODO(用一句话描述该文件做什么)
 * @author siyunlong
 * @date 2016年8月13日 下午12:20:49
 * @version V1.0
 */
public class CardUtils {

	public static Vector<Card> createSHRoomCards(int num) {
		RoomSetting rs=new RoomSetting();
		
		if (rs.isHasFeng()) {
			return createCards();
		}else		
		return createCardsNoFeng();
	}
	/*
	 * 麻将固定发牌
	 */

	public static Vector<Card> createTestCards() {
		StringBuilder sb = new StringBuilder();
		//七对
		sb.append("1-1;1-2;1-3;1-4;1-5;1-6;1-7;1-1;1-2;1-3;1-4;1-5;1-6;1-7;");
		// 发的牌
		sb.append("1-1;1-2;1-3;1-4;1-5;1-6;1-7;1-8;1-9;");
		sb.append("4-0;4-0;4-0;4-0;9-0;");
		sb.append("1-1;1-2;1-3;1-4;1-5;1-6;1-7;1-8;1-9;");
		sb.append("5-0;5-0;5-0;8-0;");
		sb.append("1-1;1-2;1-3;1-4;1-5;1-6;1-7;1-8;1-9;");
		sb.append("6-0;6-0;6-0;8-0;");
		sb.append("1-1;1-2;1-3;1-4;1-5;1-6;1-7;1-8;1-9;");
		sb.append("9-0;5-0;6-0;4-0;");
		// -----------------------------------摸的牌
		sb.append("9-0;9-0;8-0;8-0;");
		sb.append("2-1;2-2;2-3;2-4;2-5;2-6;2-7;2-8;2-9;");
		sb.append("2-1;2-2;2-3;2-4;2-5;2-6;2-7;2-8;2-9;");
		sb.append("2-1;2-2;2-3;2-4;2-5;2-6;2-7;2-8;2-9;");
		sb.append("2-1;2-2;2-3;2-4;2-5;2-6;2-7;2-8;2-9;");
		sb.append("3-1;3-2;3-3;3-4;3-5;3-6;3-7;3-8;3-9;");
		sb.append("3-1;3-2;3-3;3-4;3-5;3-6;3-7;3-8;3-9;");
		sb.append("3-1;3-2;3-3;3-4;3-5;3-6;3-7;3-8;3-9;");
		sb.append("3-1;3-2;3-3;3-4;3-5;3-6;3-7;3-8;3-9;");
		// sb.append("4-0;4-0;4-0;4-0;");
		// sb.append("5-0;5-0;5-0;5-0;");
		// sb.append("6-0;6-0;6-0;6-0;");
		// sb.append("7-0;7-0;7-0;7-0;");
		// sb.append("8-0;8-0;8-0;8-0;");
		// sb.append("9-0;9-0;9-0;9-0;");
		sb.append("10-0;10-0;10-0;10-0;");
		return testCards(sb.toString());
	}
	// 麻将生成牌 根据有没有风牌
	public static Vector<Card> createSHRoomCards(boolean isHaveFeng) {
		// System.out.println("ishavefeng:" + isHaveFeng);
		return isHaveFeng ? createCards() : createCardsNoFeng();

	}
	//麻将生成牌
	private static Vector<Card> createCards() {
		Vector<Card> vec = new Vector<>(136);
		//生成万、筒、条
		for (int i = CardType.WAN; i <= CardType.BING; i++) {
			for (int j = 1; j < 10; j++) {
				for (int k = 0; k < 4; k++) {
					vec.add(new Card(i, j));
				}
			}
		}
		//风 三
		for (int i = CardType.DONG; i <= CardType.BAI; i++) {
			for (int k = 0; k < 4; k++) {
				vec.add(new Card(i, 0));
			}
		}
//		for (int j = 1; j < 4; j++) {
//			for (int k = 0; k < 4; k++) {
//				vec.add(new Card(CardType.Jian, j));
//			}
//		}
		Collections.shuffle(vec);
		return vec;
	}
	//麻将生成牌生成万、筒、条
	public static Vector<Card> createHZCards() {
		Vector<Card> vec = new Vector<>(136);
		// 生成万、筒、条
		for (int i = CardType.WAN; i <= CardType.BING; i++) {
			for (int j = 1; j < 10; j++) {
				for (int k = 0; k < 4; k++) {
					vec.add(new Card(i, j));
				}
			}
		}
		// 风 三
		for (int k = 0; k < 4; k++) {
			vec.add(new Card(CardType.ZHONG, 0));
		}
		Collections.shuffle(vec);
		return vec;
	}

	// 麻将生成牌生成万、筒、条
	private static Vector<Card> createCardsNoFeng() {
		Vector<Card> vec = new Vector<>(108);
		// 生成万、筒、条
		for (int i = CardType.WAN; i <= CardType.BING; i++) {
			for (int j = 1; j < 10; j++) {
				for (int k = 0; k < 4; k++) {
					vec.add(new Card(i, j));
				}
			}
		}
		Collections.shuffle(vec);
		return vec;
	}

	// 扎金花生成牌
	public static Vector<Card> createTGG() {
		Vector<Card> vec = new Vector<>(52);
		for (int j = 2; j <= 14; j++) {
			for (int k = 0; k < 4; k++) {
				vec.add(new Card(k, j));
			}
		}
		Collections.shuffle(vec);
		return vec;
	}
	//牛牛牌
	public static Vector<Card> createNN() {
		Vector<Card> vec = new Vector<>(52);
		for (int j = 1; j <= 13; j++) {
			for (int k = 0; k < 4; k++) {
				vec.add(new Card(k, j));
			}
		}
		Collections.shuffle(vec);
		return vec;
	}
	//斗地主牌
	public static Vector<Card> createDdz() {
		Vector<Card> vec = new Vector<>(54);
		for (int j = 3; j <= 15; j++) {// j：牌的面值
			for (int k = 0; k < 4; k++) {// k：牌的花色
				vec.add(new Card(k, j));
			}
		}
		vec.add(new Card(4, 0));// 添加小王
		vec.add(new Card(4, 1));// 添加大王
		Collections.shuffle(vec);// 打乱顺序
		return vec;
	}
//  跑得快牌
	public static Vector<Card> createPdk() {
		Vector<Card> vec = new Vector<>(48);
		for (int j = 3; j <= 15; j++) {// j:牌的值
			if (j == 14) {
				for (int k = 1; k < 4; k++) {// k:花色
					vec.add(new Card(k, j));// 添加3张A牌
				}
			}else if (j == 15){
					vec.add(new Card(0, j));// 添加1张牌值为2的牌
			}else {
				for (int k = 0; k < 4; k++) {
					vec.add(new Card(k, j));
				}
			}
		}
		Collections.shuffle(vec);// 打算顺序
		return vec;
	}
	//跑得快发牌
	public static Vector<Card> faPaiToVector(Vector<Card> cards,int count) {
		Vector<Card> outCard = new Vector<>(count);
		while(count > 0) {
			outCard.add(cards.remove(cards.size()-1));
			count--;
		}
		return outCard; 
	}
	
	public static Vector<Card> createCardsFromStr(String source) {
		Vector<Card> vec = new Vector<>();
		for (String c : source.split(";")) {
			String[] cc = c.split("-");
			vec.add(new Card(PubFunc.parseInt(cc[0]), PubFunc.parseInt(cc[1])));
		}
		return vec;
	}
	
	public static Card faPaiOne(Vector<Card> cards) {
		return cards.remove(cards.size()-1);
	}
	
	public static void removeVector(Vector<Card> sour,Vector<Card> desc) {
		for (Card card : desc) {
			sour.remove(card);
		}
	}
	
	public static Vector<Hand> findLinkByCards(Vector<Card> sour) {
		Vector<Hand> returnVector = new Vector<Hand>();
		for (int i = 0; i < sour.size(); i++) {
			if (sour.size() < 3 || sour.size() - i < 3) {
				break;
			}
			Card firstCard = sour.elementAt(i);
			Vector<Card> linkCardsTemp = new Vector<Card>();
			linkCardsTemp.add(firstCard);
			for (int j = i + 1; j < sour.size(); j++) {
				if (linkCardsTemp.size() == 3) {
					returnVector.add(new Hand((Vector<Card>)linkCardsTemp.clone()));
					removeVector(sour, linkCardsTemp);
					linkCardsTemp.clear();
					i = -1;
					break;
				}
				Card card = sour.elementAt(j);
				Card lastCard = (Card) linkCardsTemp.lastElement();
				if (card.getType() == lastCard.getType() && lastCard.getIndex() == card.getIndex() - 1) {
					linkCardsTemp.add(card);
				} else {
					continue;
				}
			}
			if (linkCardsTemp.size() == 3) {
				returnVector.add(new Hand((Vector<Card>)linkCardsTemp.clone()));
				removeVector(sour, linkCardsTemp);
				linkCardsTemp.clear();
				i = -1;
			}
		}
		return returnVector;
	}
	
	public static Vector<Hand> findSameCardBySize(Vector<Card> sour, int size) {
		Vector<Hand> desc = new Vector<>();
		int cardsCount = sour.size();
		int currentIndex = cardsCount - 1;
		while (currentIndex > -1) {
			int tempIndex = currentIndex;
			if (tempIndex < (size - 1)) {
				break;
			}
			Card[] cards = new Card[size];
			for (int i = 0; i < size; i++) {
				cards[i] = sour.elementAt(tempIndex);
				tempIndex--;
			}
			boolean isSame = true;
			for (int i = 0; i < size - 1; i++) {
				if (cards[0].getType() != cards[i + 1].getType() || cards[0].getIndex() != cards[i + 1].getIndex()) {
					isSame = false;
					break;
				}
			}
			if (isSame) {
				Vector<Card> tempVec = new Vector<Card>();
				for (int i = 0; i < size; i++) {
					tempVec.add(cards[i]);
					sour.remove(cards[i]);
				}
				desc.add(0, new Hand((Vector<Card>) tempVec.clone()));
				currentIndex -= size;
			} else {
				currentIndex--;
			}
		}
		return desc;
	}

	public static boolean isSameCard(Vector<Card> cards) {
		if(cards == null || cards.isEmpty()) 
		return false;
		if(cards.size() == 1) 
		return true;
		Card card = cards.get(0);
        for (int i = 0; i < cards.size() - 1; i++) {
           
		    if (card.getType() != cards.get(i+1).getType() || card.getIndex() != cards.get(i+1).getIndex()) {
                return false;
            }
        }
        return true;
	}

	public static Vector<Card> createHZTestCards() {
		StringBuilder sb = new StringBuilder();
		//杠后停
		sb.append("2-2;2-2;2-2;1-1;1-1;1-1;1-2;1-2;1-2;1-8;1-8;1-8;3-6;3-6;");
		sb.append("1-1;1-1;1-1;4-0;4-0;4-0;2-2;2-2;2-2;1-7;1-9;3-5;3-5;3-6;3-7;3-5;3-5;3-5;");
		sb.append(
				"3-5;3-5;3-5;2-7;3-7;3-9;1-6;1-9;3-9;2-3;3-3;3-9;3-6;1-8;1-3;1-6;3-3;1-8;2-9;1-1;1-8;2-7;2-1;3-4;3-1;2-3;3-4;3-5;2-6;3-8;1-2;3-7;1-6;2-9;2-8;3-1;1-4;3-1;2-8;1-5;2-1;3-2;1-9;2-6;1-5;1-2;1-3;3-3;2-1;2-9;3-5;2-1;1-1;3-2;1-9;3-3;2-6;3-5;3-2;3-8;3-7;1-5;3-1;1-1;2-4;1-3;3-2;2-2;2-5;3-5;3-8;1-4;1-5;1-2;2-4;2-7;2-5;2-4;2-8;3-7;");

		return testCards(sb.toString());
	}
	
	public static Vector<Card> testCards(String str) {
		Vector<Card> allCards = new Vector<>();
		String[] ss = str.split(";");
		for (int i = ss.length-1; i >= 0 ; i--) {
			String s = ss[i];
			int type = Integer.parseInt(s.split("-")[0]);
			int index = Integer.parseInt(s.split("-")[1]);
			Card c = new Card(type, index);
			allCards.add(c);
		}
		return allCards;
	}

	public static Vector<Card> AnPai = new Vector<>(
			Lists.newArrayList(new Card(-1, 0), new Card(-1, 0), new Card(-1, 0)));
	public static Vector<Card> AnPai5 = new Vector<>(
			Lists.newArrayList(new Card(-1, 0), new Card(-1, 0), new Card(-1, 0), new Card(-1, 0), new Card(-1, 0)));

	
	public static void printCards(Vector<Card> cards) {
		StringBuilder sb = new StringBuilder();
		for (Card card : cards) {
			sb.append(card.getType()+"-"+card.getIndex());
			sb.append(";");
		}
		System.out.println(sb.toString());
	}
	
	public static void testTing() {
		for (int i = 0; i < 10000000; i++) {
//			Vector<Card> allCards = CardUtils.createCards();
//			Vector<Card> outCard = CardUtils.faPaiToVector(allCards, 14);
//			Collections.sort(outCard);
//			if(HandUtils.isHuPai(outCard)) {
//				printCards(outCard);
//				List<Card> ccs = KdUtils.getOutCardForTing(outCard);
//				printCards(allCards);
//				return;
//			}
		}
	}	
	public static void main(String[] args) {
		Vector<Card> cards = createTGG();
		System.err.println(cards);
	}
}
